<?php

class PageHelper {
    
    function select($current, $total){
        $current = empty($current)? 1 : $current;
        $total = empty($total)? 1 : intval($total);
        
        $o = "<select class=\"page_selector\">\n";
        $p = 1;
        for($p = 1; $p <= $total; $p++){
            $o .= "<option".($p == $current?' selected="selected"':'').">$p</option>\n";
        }
        $o .= "</select>\n";
        return $o;
    }
    
    function prevNext($current, $total = 1, $options = array()){
        $current = empty($current)? 1 : $current;
        $total = empty($total)? 1 : intval($total);
        
        if($current == 1 && $total == 1) return '';
        
        $max = isset($options['max_buttons'])?$options['max_buttons']:7;
        $first = max(1, $current - intval($max/2));
        $last = min($total, $first + $max - 1);
        
        if($first <> 1) $o .= '<span class="pager top_page" page="1"> << </span>';
        
        for($p = $first; $p <= $last; $p++){
            $o .= '<span class="pager page '.($p == $current?'current':'').'" page="'.$p.'"> '.$p.' </span>';
        }
        
        if($last < $total) $o .= '<span class="pager last_page" page="'. $total .'"> >> </span>';
        
        return $o;
    }

    function generateSlug($phrase, $maxLength = 50)
    {
        $result = PageHelper::normalize($phrase);
        $result = strtolower($result);

        $result = preg_replace("/[^a-z0-9\s-]/", "", $result);
        $result = trim(preg_replace("/[\s-]+/", " ", $result));
        $result = trim(substr($result, 0, $maxLength));
        $result = preg_replace("/\s/", "-", $result);

        return $result;
    }

    function normalize ($string) {
        $table = array(
            'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
            'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
            'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
            'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
            'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
            'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
            'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
            'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
        );

        return strtr($string, $table);
    }
}
